#! /usr/bin/env python
# -*- coding: utf-8 -*-

from playwright.sync_api import Page
from ui_automation_test.common.base_page import BasePage
from ui_automation_test.locators.dualPrevention.riskGrading.riskObject.loc_risk_object_add import LocAddObject
from ui_automation_test.locators.dualPrevention.riskGrading.riskObject.loc_risk_object_edit import LocEditObject
from ui_automation_test.locators.dualPrevention.riskGrading.riskObject.loc_risk_object_search import LocSearchObject
from ui_automation_test.locators.dualPrevention.riskGrading.riskObject.loc_risk_object_view import LocViewObject
from ui_automation_test.locators.dualPrevention.riskGrading.riskObject.loc_risk_object_delete import LocDeleteObject
from ui_automation_test.common.logger import logger


class AddRiskObject(BasePage):

    def __init__(self, page: Page):
        super().__init__(page)
        self.switch_menu('双重预防机制')
        self.switch_menu('风险分析清单管理')
        self.switch_menu('风险分析对象')

    def add_object(self, object_name, object_type=None, hidden_type=None, dept_name=None, person_name=None,
                   source_name=None):
        logger.info('开始风险分析对象新增测试')
        logger.info('点击新增按钮')
        # 只点击第一个"新增"按钮，避免匹配到表格内的"新增风险单元"按钮
        self.page.get_by_role('button', name='新增').first.click()
        logger.info('输入风险分析对象名称')
        self.fill(LocAddObject.loc_object_name, object_name)
        logger.info('选择风险分析对象类型')
        self.select(LocAddObject.loc_object_type, object_type)
        logger.info('选择是否重大危险源')
        self.check(LocAddObject.loc_hidden_source_or_not, hidden_type)
        if hidden_type == '是':
            logger.info('选择重大危险源')
            self.select(LocAddObject.loc_hidden_source_name, source_name)
        logger.info('选择责任部门')
        self.select(LocAddObject.loc_duty_dept, dept_name)
        logger.info('选择责任人')
        self.select(LocAddObject.loc_duty_person, person_name)
        logger.info('提交')
        self.click(LocAddObject.loc_submit)

    def edit_object(self, object_name, object_type=None, hidden_type=None, dept_name=None, person_name=None,
                   source_name=None):
        logger.info('开始风险分析对象编辑测试')
        logger.info('点击编辑按钮')
        self.click(LocEditObject.loc_edit_button)
        logger.info('输入风险分析对象名称')
        self.fill(LocEditObject.loc_object_name, object_name)
        logger.info('选择风险分析对象类型')
        self.select(LocEditObject.loc_object_type, object_type)
        logger.info('选择是否重大危险源')
        self.check(LocEditObject.loc_hidden_source_or_not, hidden_type)
        if hidden_type == '是':
            logger.info('选择重大危险源')
            self.select(LocEditObject.loc_hidden_source_name, source_name)
        logger.info('选择责任部门')
        self.select(LocEditObject.loc_duty_dept, dept_name)
        logger.info('选择责任人')
        self.select(LocEditObject.loc_duty_person, person_name)
        logger.info('提交')
        self.click(LocEditObject.loc_submit)

    def view_object(self, object_name):
        logger.info('开始风险分析对象查看测试')
        logger.info('点击查看按钮')
        self.click(LocViewObject.loc_view_button)
        logger.info('查看风险分析对象详情')
        # 先定位到卡片区域
        card = self.page.locator('div:has-text("风险分析对象信息")')
        # 只选卡片内 class 为 el-button--primary 且文本为“返回”的按钮
        back_btn = card.locator('button.el-button--primary', has_text="返回")
        back_btn.scroll_into_view_if_needed()
        back_btn.click()


    def search_object(self, object_name=None, company=None, major_hazard=None, object_type=None, dept=None):
        logger.info('开始风险分析对象查询')
        if company:
            logger.info(f'选择所属企业: {company}')
            self.select(LocSearchObject.loc_company_select, company)
        if object_name:
            logger.info(f'选择对象名称: {object_name}')
            self.select(LocSearchObject.loc_object_name_select, object_name)
        if major_hazard:
            logger.info(f'选择是否重大危险源: {major_hazard}')
            self.select(LocSearchObject.loc_major_hazard_select, major_hazard)
        if object_type:
            logger.info(f'选择对象类型: {object_type}')
            self.select(LocSearchObject.loc_object_type_select, object_type)
        if dept:
            logger.info(f'选择责任部门: {dept}')
            self.select(LocSearchObject.loc_dept_select, dept)
        logger.info('点击查询按钮')
        self.click(LocSearchObject.loc_search_button)



    def delete_object_by_name(self, object_name):
        """
        根据对象名称删除风险分析对象
        :param object_name: 风险分析对象名称
        """
        logger.info(f'开始删除对象: {object_name}')
        # 找到包含目标对象名的行，点击对应的删除按钮
        row = self.page.locator('.el-table__body tr').filter(has_text=object_name).nth(0)
        logger.info('点击删除按钮')
        row.get_by_text('删除').click()
        logger.info('确认删除')
        self.click(LocDeleteObject.loc_confirm_delete_button)
        logger.info(f'删除对象 {object_name} 完成')

    